<?php
  include("include/conn.php");
  require('include/Smarty/libs/Smarty.class.php');
  
  $msg = '';
  $total = array();
  $bhatthiFromDate = "";
  if(isset($_POST['submit']))
  {
  	$partyId             = isset($_REQUEST['partyIdEdit']) ? $_REQUEST['partyIdEdit'] : 0;
	  $bhatthipartyName    = isset($_REQUEST['bhatthipartyName']) ? $_REQUEST['bhatthipartyName'] : 0;
	  $bhatthiAddress      = isset($_REQUEST['bhatthiAddress']) ? $_REQUEST['bhatthiAddress'] : 0;
	  $partyMobile         = isset($_REQUEST['partyMobile']) ? $_REQUEST['partyMobile'] : 0;
	  $bhatthiFromDate     = isset($_REQUEST['toDateYear']) ? $_REQUEST['toDateYear']."-".$_REQUEST['toDateMonth']."-".$_REQUEST['toDateDay'] : 0;
	  echo $updateQueryBhatthiParty = "UPDATE bhatthiparty 
                                   SET bhatthipartyName  = '".$bhatthipartyName."',
                                       bhatthiAddress    = '".$bhatthiAddress."',
                                       partyMobile       = '".$partyMobile."',
                                       bhatthiFromDate   = '".$bhatthiFromDate."'
                                 WHERE bhatthipartyId = '".$_REQUEST['bhatthipartyIdEdit']."'";
	  $updateQueryBhatthiPartyRequest = mysql_query($updateQueryBhatthiParty);
	  if(!$updateQueryBhatthiPartyRequest)
	  {
	    die("Update Query Not Inserted : ".mysql_error());
	  }
	  else
	  {
	 		header("Location:bhatthiPartyList.php");
	  }
	}
	
	$bhatthipartyId      = '';
	$bhatthipartyName    = '';
	$selectQueryBhatthiParty = "SELECT bhatthipartyId,bhatthipartyName 
	                              FROM bhatthiparty
	                             WHERE bhatthipartyId = ".$_REQUEST['bhatthipartyId'];
	$selectQueryBhatthiPartyResult = mysql_query($selectQueryBhatthiParty);
	$selectQueryBhatthiPartyNumRow = mysql_num_rows($selectQueryBhatthiPartyResult);
	if($selectQueryBhatthiPartyNumRow > 0)
	{
		while($bhatthiPartyRow = mysql_fetch_array($selectQueryBhatthiPartyResult))
		{
			$bhatthipartyId = $bhatthiPartyRow['bhatthipartyId'];
			$bhatthipartyName = $bhatthiPartyRow['bhatthipartyName'];
		}
	}

  if(isset($_POST['party']) && $_POST['party'] > 0)
  {	
    // Selection Of Net Fine And Net Amount :Start
    $partyId   = $_POST['party'];
    $toDate    = $_POST['toDateYear']."-".$_POST['toDateMonth']."-".$_POST['toDateDay'];
    $bhatthiBookDetail = "SELECT bhatthibookdetail.bhatthiBookDetailId, bhatthibook.bhatthiBookId, bhatthibook.bhatthiPartyId, DATE_FORMAT(bhatthibook.bhatthiDate,'%Y-%m-%d') As bhatthiDate, 
                                 bhatthibookdetail.particulars,bhatthibookdetail.weight, bhatthibookdetail.touch, bhatthibookdetail.fine, bhatthibookdetail.qty, 
                                 bhatthibookdetail.rate, bhatthibookdetail.amount, bhatthibookdetail.CrDr
                            FROM bhatthibook
                            JOIN bhatthibookdetail ON bhatthibookdetail.bhatthiBookId = bhatthibook.bhatthiBookId
                           WHERE bhatthibook.bhatthiPartyId = ".$_POST['party']."
                             AND bhatthibook.bhatthiDate <= '".$toDate."'
                           ORDER BY bhatthibook.bhatthiDate, bhatthibookdetail.bhatthiBookDetailId";
    $bhatthiBookDetailRes = mysql_query($bhatthiBookDetail);
    $drCounter            = 0;
    $crCounter            = 0;
    
    $total['drWeight']   = 0;
    $total['drTouch']    = 0;
    $total['drFine']     = 0;
    $total['drAmount']   = 0;
    $total['crFine']     = 0;
    $total['crQty']      = 0;
    $total['crAmount']   = 0;
    
    $opening['crWeight'] = 0; 
    $opening['drWeight'] = 0; 
    $opening['crTouch']  = 0; 
    $opening['drTouch']  = 0; 
    $opening['crFine']   = 0; 
    $opening['drFine']   = 0; 
    $opening['crQty']    = 0; 
    $opening['crRate']   = 0; 
    $opening['crAmount'] = 0; 
    $opening['drAmount'] = 0; 
    
    while($bhatthiRow = mysql_fetch_assoc($bhatthiBookDetailRes))
    {
      if($bhatthiRow['bhatthiDate'] <= $toDate)
      {
        if($bhatthiRow['CrDr'] == 'Dr')
        {
          $bhatthiBookDr[$drCounter]['bhatthiBookDetailId'] = $bhatthiRow['bhatthiBookDetailId'];
          $bhatthiBookDr[$drCounter]['bhatthiBookId'] = $bhatthiRow['bhatthiBookId'];
          $bhatthiBookDr[$drCounter]['date']          = $bhatthiRow['bhatthiDate'];
          $bhatthiBookDr[$drCounter]['particulars']   = $bhatthiRow['particulars'];
          $bhatthiBookDr[$drCounter]['weight']       = $bhatthiRow['weight'];
          $bhatthiBookDr[$drCounter]['touch']        = $bhatthiRow['touch'];
          $bhatthiBookDr[$drCounter]['fine']          = $bhatthiRow['fine'];
          $bhatthiBookDr[$drCounter]['amount']        = $bhatthiRow['amount'];
          $drCounter++;
        }
        else
        {
          $bhatthiBookCr[$crCounter]['bhatthiBookDetailId'] = $bhatthiRow['bhatthiBookDetailId'];
          $bhatthiBookCr[$crCounter]['bhatthiBookId'] = $bhatthiRow['bhatthiBookId'];
          $bhatthiBookCr[$crCounter]['date']          = $bhatthiRow['bhatthiDate'];
          $bhatthiBookCr[$crCounter]['particulars']   = $bhatthiRow['particulars'];
          $bhatthiBookCr[$crCounter]['weight']        = $bhatthiRow['weight'];
          $bhatthiBookCr[$crCounter]['touch']         = $bhatthiRow['touch'];
          $bhatthiBookCr[$crCounter]['fine']          = $bhatthiRow['fine'];
          $bhatthiBookCr[$crCounter]['qty']           = $bhatthiRow['qty'];
          $bhatthiBookCr[$crCounter]['rate']          = $bhatthiRow['rate'];
          $bhatthiBookCr[$crCounter]['amount']        = $bhatthiRow['amount'];
          $crCounter++;
        }
      }
      else
      {  // Set opening : Start
        if($bhatthiRow['CrDr'] == 'Dr')
        {
          $opening['drFine']   += $bhatthiRow['fine'];
          $opening['drAmount'] += $bhatthiRow['amount'];
        }
        else
        {
          $opening['crWeight'] += $bhatthiRow['weight'];
          $opening['crTouch']  += $bhatthiRow['touch'];
          $opening['crFine']   += $bhatthiRow['fine'];
          $opening['crQty']    += $bhatthiRow['qty'];
          $opening['crRate']   += $bhatthiRow['rate'];
          $opening['crAmount'] += $bhatthiRow['amount'];
        }
      }// Set opening : End
      
      // this for total :Start
      if($bhatthiRow['CrDr'] == 'Dr')
      {
        $total['drFine']   += $bhatthiRow['fine'];
        $total['drAmount'] += $bhatthiRow['amount'];
      }
      else
      {
        $total['crFine']   += $bhatthiRow['fine'];
        $total['crQty']    += $bhatthiRow['qty'];
        $total['crAmount'] += $bhatthiRow['amount'];
      }
      $total['totFine']   = ($total['drFine'] - $total['crFine']);
      $total['totAmount'] = ($total['drAmount'] - $total['crAmount']);
      // this for total :End
    }
  }
    // Selection Of Net Fine And Net Amount :End	
  
  // Delete Record All Data With Selected Party :Start
 	if(isset($_POST['delete']))
	{
		$partyId   = isset($_POST['party']) ? $_POST['party'] : 0;
		$bhatthiDate     = isset($_REQUEST['toDateYear']) ? $_REQUEST['toDateYear']."-".$_REQUEST['toDateMonth']."-".$_REQUEST['toDateDay'] : 0;
	  //Delete Records Before INSERT Opening :Start
    $deleteQueryDetailBhatti =  "DELETE FROM bhatthibookdetail
                                   WHERE bhatthiBookId IN 
                                         (SELECT bhatthiBookId FROM bhatthibook WHERE partyId = ".$partyId.")";
    $deleteQueryDetailBhattiRes = mysql_query($deleteQueryDetailBhatti);
    // Master Delete :Start
    $deleteQueryBhatthi   = "DELETE FROM  bhatthibook
                              WHERE bhatthiPartyId = ".$partyId."
                                AND bhatthiDate <= '".$bhatthiDate."'";
    $deleteQueryBhatthiRes  = mysql_query($deleteQueryBhatthi);
    // Master Delete :Start
    //Delete Records Before INSERT Opening :Start
  }
  // Delete Record All Data With Selected Party :End
  
  $smarty = new Smarty;
  require('bottom.php');
  $smarty->assign("l_editBhatthiParty",$l_editBhatthiParty);
  $smarty->assign("l_bhatthipartyName",$l_bhatthipartyName);
  $smarty->assign("bhatthipartyId",$bhatthipartyId);
  $smarty->assign("bhatthipartyName",$bhatthipartyName);
  $smarty->assign("bhatthiFromDate",$bhatthiFromDate);
  $smarty->assign("total",$total);
	$smarty->display('bhatthiPartyEdit.tpl');
?>
